//
//  MainViewController.swift
//  elarc01
//
//  Created by elarc on 16/2/26.
//  Copyright © 2016年 elarc. All rights reserved.
//

import UIKit
import TransitionTreasury
import TransitionAnimation

class MainViewController: UITabBarController,TRTabBarControllerDelegate {
    
    private var adImageView: UIImageView?
    var adImage: UIImage? {
        didSet {
            weak var tmpSelf = self
            adImageView = UIImageView(frame: ScreenBounds)
            adImageView!.image = adImage!
            self.view.addSubview(adImageView!)
            
            UIImageView.animateWithDuration(2.0, animations: { () -> Void in
                tmpSelf!.adImageView!.transform = CGAffineTransformMakeScale(1.2, 1.2)
                tmpSelf!.adImageView!.alpha = 0
                }) { (finish) -> Void in
                    tmpSelf!.adImageView?.removeFromSuperview()
                    tmpSelf!.adImageView = nil
            }
        }
    }
    
    func tr_tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        print("You did select \(viewController.dynamicType).")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tr_transitionDelegate = TRTabBarTransitionDelegate(method: TRTabBarTransitionMethod.Slide)
        self.tr_delegate = self
        //kvc 去掉系统TabBar
        let tb = MainTabBar()
        setValue(tb, forKey: "tabBar")
        addChildViewControllers()
        tb.compoesButton.addTarget(self, action: #selector(MainViewController.composedBttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        // Do any additional setup after loading the view.
    }
    
    //点击加号方法
    func composedBttonClicked(sender:UIButton){
        Animations.showAnimationOnclickHeart(sender)
        let nav = UINavigationController(rootViewController: ComposeViewController())
        
        presentViewController(nav, animated: true, completion: nil)
        print(#function)
    }
    
    //添加所有子控制器
    private func addChildViewControllers(){
        let originImages = ["tabbar_home","tabbar_discover","tabbar_set","tabbar_profile"]
        var imageNameS = []
        #if DEVELOPMENT
            imageNameS = originImages.map{"\($0)_dev"}
        #else
            imageNameS = originImages
        #endif
        
        addChildViewController(IPTableViewController(), title: "首页", imageName: imageNameS[0] as! String)
        addChildViewController(SearchViewController(), title: "搜索", imageName:imageNameS[1] as! String)
        addChildViewController(PageHomeController(), title: "设置", imageName:imageNameS[2] as! String)
        addChildViewController(NewIdealViewController(), title: "新特性", imageName: imageNameS[3] as! String)
    }
    //添加控制器
    private func addChildViewController(vc:UIViewController, title:String,imageName:String){
        
        tabBar.tintColor = UIColor.randomColor()
        vc.title = title
        vc.tabBarItem.selectedImage = UIImage(named:"tab_highlighted")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        vc.tabBarItem.image = UIImage(named: imageName)!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
        tabBarController?.selectedIndex = 0
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}
